import { reqAddOrEdit, reqDelete, reqTmList } from "@/api";

export default {
  namespaced: true, //开启命名空间
  state: {
    tmList: [], //品牌列表
    total: 0, // 品牌总数
  },
  mutations: {
    UPDATE_TMLIST(state, data) {
      state.tmList = data.records;
      state.total = data.total;
    },
  },
  actions: {
    //获取品牌数据
    async getTmList({ commit }, { page, limit }) {
      try {
        const res = await reqTmList(page, limit);
        if (res.code === 200) {
          commit("UPDATE_TMLIST", res.data);
          return "ok";
        }
      } catch (error) {
        return Promise.reject("fail");
      }
    },
    // 添加或修改
    async getAddOrEdit({ commit }, tm) {
      try {
        const res = await reqAddOrEdit(tm);
        if (res.code === 200) {
          return "ok";
        }
      } catch (error) {
        return Promise.reject("fail");
      }
    },
    //删除
    async getDeleteTrademark({ commit }, id) {
      try {
        const res = await reqDelete(id);
        if (res.code === 200) {
          return "ok";
        }
      } catch (error) {
        return Promise.reject("fail");
      }
    },
  },
  getters: {},
};
